<template>
  <div class="component-container">
    <div class="component-container__main">
      <el-card class="box-card">
        <template #header>
          <span>商品属性</span>
          <el-button style="float: right" type="success" size="small" @click="handleAdd"
            ><i-ep-plus />
            添加属性
          </el-button>
        </template>
        <el-form :model="goodsInfo" size="small" ref="dataFormRef">
          <el-table :data="goodsInfo.attrList" size="small" highlight-current-row border>
            <el-table-column prop="name" label="属性名称">
              <template #default="scope">
                <el-form-item>
                  <el-input v-model="scope.row.name" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="value" label="属性值">
              <template #default="scope">
                <el-form-item>
                  <el-input v-model="scope.row.value" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template #default="scope">
                <el-form-item>
                  <el-button
                    v-if="scope.$index > 0"
                    type="danger"
                    size="small"
                    circle
                    plain
                    @click.stop="handleRemove(scope.$index)"
                    ><i-ep-minus
                  /></el-button>
                </el-form-item>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </el-card>
    </div>
    <div class="component-container__footer">
      <el-button @click="handlePrev">上一步，填写商品信息</el-button>
      <el-button type="primary" @click="handleNext">下一步，设置商品库存</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getAttributeList } from '@/api/pms/attribute'
const emit = defineEmits(['prev', 'next', 'update:modelValue'])
const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {
      return {}
    }
  }
})

const dataFormRef = ref(ElForm)

const goodsInfo: any = computed({
  get: () => props.modelValue,
  set: (val: any) => emit('update:modelValue', val)
})

const handleAdd = () => {
  goodsInfo.value.attrList.push({})
}

watch(
  () => goodsInfo.value.categoryId,
  (newVal) => {
    const goodsId = goodsInfo.value.id
    if (goodsId) {
      return false
    }
    if (newVal) {
      getAttributeList({ categoryId: newVal, type: 2 }).then((res: any) => {
        const attrList = res.data
        if (attrList && attrList.length > 0) {
          goodsInfo.value.attrList = attrList
        } else {
          goodsInfo.value.attrList = [{}]
        }
      })
    } else {
      goodsInfo.value.attrList = [{}]
    }
  },
  {
    immediate: true,
    deep: true
  }
)

const handleRemove = (index: number) => {
  goodsInfo.value.attrList.splice(index, 1)
}

const handlePrev = () => {
  emit('prev')
}

const handleNext = () => {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      emit('next')
    }
  })
}
</script>


<style scoped lang="scss">
.component-container {
  &__main {
    margin: 20px auto;
  }

  &__footer {
    position: fixed;
    right: 20px;
    bottom: 20px;
  }
}

.el-form-item--mini.el-form-item {
  margin-top: 18px;
}
</style>